<?php
@include ("includes/gpc_function.php");

$type       = isset($_GET["type"]) ? strtolower($_GET["type"])	 : "";
$id   		= isset($_GET["id"])  ?  	$_GET["id"]          	 : 0;
$cookie     = gpc_getCookieArray("imgtr");

switch($type)
{
	case "yahoo":
		CheckYahoo($id);
		break;
	case "skype":
		CheckSkype($id);
		break;
   case "wmsg":
        CheckWmsg($id);
        break;
   case 'spam':
        CreateSpamImg();
        break;
   case 'cr':
        CreateImage($id);
        break;
	default:
		echo "Not support!";
}

function CreateSpamImg()
{
    require("./includes/class_captcha.php");		 
    $aFonts = array('font/VeraSeBd.ttf');				 
    $oVisualCaptcha = new PhpCaptcha($aFonts, 140, 25);
    $oVisualCaptcha->UseColour(true);	 
    $oVisualCaptcha->Create();
}
function CheckYahoo($id)
{
    global $cookie;
	// seconds, minutes, hours, days
	$expires = 60*5*1*1;
	//header("Pragma: public");
	header("Cache-Control: maxage=".$expires);
	header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
	header("content-type: image/gif");
    header('ETag: "f4d030ef8f7ccb1:22665"');

    $iLastModf  = intval($cookie['ylm']);
    if($iLastModf<time()-$expires)
    {
        $link="http://opi.yahoo.com/online?u=".$id."&m=t";
        $data=file_get_contents($link);
        $cookie['ylm']=time();
    }else
    {
        if($cookie['yol']==1)
        {
            $data="";
        }else
        {
            $data="NOT ONLINE";
        }
    }
	//echo strlen($data);
	if(stripos($data,"NOT ONLINE")===false)
	{
	   $cookie['yol']=1;
	   $file="images/yahoo_on.gif";
	}else
	{
	   $cookie['yol']=0;
	   $file="images/yahoo_off.gif";
	}
    gpc_setCookieArray("imgtr",$cookie,0,0,"/Img/");
	echo file_get_contents($file);
}


function CheckSkype($id)
{
    global $cookie;
	// seconds, minutes, hours, days
	$expires = 60*5*1*1;

	//header("Pragma: public");
	header("Cache-Control: maxage=".$expires);
	header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
	header("content-type: image/gif");
    header('ETag: "f4d030ef8f7ccb1:22665"');

    $iLastModf  = intval($cookie['SK'.$id]);
    if($iLastModf<time()-$expires)
    {
        $link="http://mystatus.skype.com/smallclassic/".$id;
        $data=file_get_contents($link);
        $cookie['SK'.$id]=time();
    }else
    {
        if($cookie['sko'.$id]==1)
        {
            $data="";
        }else
        {
            $data="NOT ONLINE";
        }
    }

	if(strlen($data)>1000 || empty($data))
	{
	   $cookie['sko'.$id]=1;
		$file="images/skype_on.gif";
	}else
	{
	   $cookie['sko'.$id]=0;
		$file="images/skype_off.gif";
	}
    gpc_setCookieArray("imgtr",$cookie,0,0,"/Img/");
	echo file_get_contents($file);
}

function CheckWmsg($id)
{
    global $cookie;
	// seconds, minutes, hours, days
	$expires = 60*5*1*1;

	//header("Pragma: public");
	header("Cache-Control: maxage=".$expires);
	header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
	header("content-type: image/gif");
    header('ETag: "f4d030ef8f7ccb1:22665"');

   $file    = "images/wmsg.gif";
    
	echo file_get_contents($file);
}

function CreateImage($string,$fontsize=14,$height=50,$font='BaroqueScript',$color='A25193',$background='ffffff',$tran=1)
{
    $expires        = 60*10*1*1;
    $string         = urldecode($string);
    $string         = str_replace("\\","",$string);
    $string         = str_replace(".","&",$string);
    $fontType       = "font/".$font.".ttf";
      
    //parse color 
    $rt             = hexdec(substr($color,0,2));
    $gt             = hexdec(substr($color,2,2));
    $bt             = hexdec(substr($color,4,2));
    //parse bgcolor
    $rbg            = hexdec(substr($background,0,2));
    $gbg            = hexdec(substr($background,2,2));
    $bbg            = hexdec(substr($background,4,2));
    
    //cal size image:
    $width          = ($fontsize+5)*strlen($string)/2 + $fontsize*$fontsize/2;
    $text_x         = 0;
    $text_y         = $height*2/3+2;
    
    //create image
    $img            = imagecreate($width,$height);
    $bg_color       = imagecolorallocate($img, $rbg,$gbg,$bbg);
    $text_color     = imagecolorallocate($img, $rt,$gt,$bt);    
    
    imagecolortransparent($img, $bg_color);
    imagettftext ($img, $fontsize,0, $text_x, $text_y, $text_color, $fontType, $string);
    
    $widthnew       = $width;
    for($x=intval($width);$x>0;$x-=2)
    {
    	for($y=intval($height)-3;$y>3;$y--)
    	{
    		$rgb      = imagecolorat($img,$x,$y);
    		$colors   = imagecolorsforindex($img, $rgb);
    		if($rbg  !=$colors['red'] || $gbg!=$colors['green'] || $bbg!=$colors['blue'])
    		{
    			$widthnew    = $x+4;
    			break(2);
    		}
    	}
    }
    
    $img2           = imagecreatetruecolor($widthnew,$height);
    imagecopy($img2, $img, 0, 0, 0, 0, $widthnew, $height);
    imagecolortransparent($img2, $background_color);
    
    header("Pragma: public");
    header("Cache-Control: maxage=".$expires);
    header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');

    //send type
    header("content-type: image/gif");
    
    imagegif($img2);
    imagedestroy($img);
    imagedestroy($img2);
}
?>
